package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.auth.Perm;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IPermDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
@Repository("permDao")
public class PermDaoImpl implements IPermDao {

	@Resource(name = "baseDao")
	IBaseDao<Perm> baseDao;
	
	@Override
	public void save(Perm perm) {
		// TODO Auto-generated method stub
		baseDao.save(perm);
	}

	@Override
	public void update(Perm perm) {
		// TODO Auto-generated method stub
		baseDao.update(perm);
	}

	@Override
	public long rowCount() {
		// TODO Auto-generated method stub
		short status = -1;
		return baseDao.countAll("Perm", status, null);
	}

	@Override
	public Perm queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(Perm.class, id);
	}

	@Override
	public Perm queryByPermName(String name) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from Perm where ");
		hql.append("permission='" + name + "'");
		List<Perm> resultList = baseDao.query(hql.toString());
		if (resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void delete(Perm perm) {
		// TODO Auto-generated method stub
		baseDao.delete(perm);
	}

	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		baseDao.delById(Perm.class, id);
	}

	@Override
	public List<Perm> queryAll() {
		// TODO Auto-generated method stub
		return baseDao.queryList("Perm");
	}

	@Override
	public PageBean<Perm> queryByPage(int pageNum, int pageSize)  throws DBException{
		// TODO Auto-generated method stub
		PageBean<Perm> bean = baseDao.queryByHqlForPage(pageNum, pageSize, "Perm");
		return bean;
	}

}
